//
//  YDStockMarketDiskDataView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/1/19.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDStockMarketDiskDataView.h"


@interface YDStockMarketDiskDataView()
//今开
@property (nonatomic , weak) UILabel *openPrice;
//昨收
@property (nonatomic , weak) UILabel *yesClosePrice;
//最高
@property (nonatomic , weak) UILabel *maxPrice;
//最低
@property (nonatomic , weak) UILabel *minPrice;
//振幅
@property (nonatomic , weak) UILabel *priceSpeed;
//成交额
@property (nonatomic , weak) UILabel *value;
//成交量
@property (nonatomic , weak) UILabel *volume;

/////间距属性
@property (nonatomic , assign) CGFloat marginLR;//左右边框间距
@property (nonatomic , assign) CGFloat marginTB;//控件上下间距
@property (nonatomic , assign) CGFloat margin;//控件左右间距
@property (nonatomic , assign) CGFloat labelWidth;//控件宽度

@end

@implementation YDStockMarketDiskDataView

-(void)setStockDict:(NSDictionary *)stockDict
{
    _stockDict = stockDict;
    BOOL isUp;
    if (stockDict[@"upDropSpeed"] && [stockDict[@"upDropSpeed"] floatValue] >0) {
        isUp = YES;
    }else{
        isUp = NO;
    }
    
    
    //今开  左收
    NSString *openPriceString;
    NSAttributedString *aopenPriceString;
    if (stockDict[@"openPrice"]) {
        NSString *openPrice = [[NSString stringWithFormat:@"%@" , stockDict[@"openPrice"]] pz_stringNumberByMultiplyingBy:@"1"];
        openPriceString = [NSString stringWithFormat:@"%@ %@" , @"今 开" , openPrice];
        if (isUp) {
            aopenPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , openPrice] changeColor:[UIColor colorWithHexString:stockRed] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:openPriceString];
        }else{
            aopenPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , openPrice] changeColor:[UIColor colorWithHexString:stockGreen] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:openPriceString];
        }
        
    }else{
        openPriceString = [NSString stringWithFormat:@"%@ %@" , @"今 开" , @"---"];
        if (isUp) {
            aopenPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:stockRed] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:openPriceString];
        }else{
            aopenPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:stockGreen] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:openPriceString];
        }
    }
    self.openPrice.attributedText = aopenPriceString;
    
    NSString *preClsPriceString;
    NSAttributedString *apreClsPriceString;
    if (stockDict[@"preClsPrice"]) {
        NSString *preClsPrice = [[NSString stringWithFormat:@"%@" , stockDict[@"preClsPrice"]] pz_stringNumberByMultiplyingBy:@"1"];
        preClsPriceString = [NSString stringWithFormat:@"%@ %@" , @"昨 收" , preClsPrice];
        
        apreClsPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , preClsPrice] changeColor:[UIColor colorWithHexString:dark] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:preClsPriceString];
    }else{
        preClsPriceString = [NSString stringWithFormat:@"%@ %@" , @"昨 收" , @"---"];
        apreClsPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:dark] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:preClsPriceString];
    }
    self.yesClosePrice.attributedText = apreClsPriceString;
    
    //最高  最低
    NSString *highestPriceString;
    NSAttributedString *ahighestPriceString;
    if (stockDict[@"highestPrice"]) {
        NSString *highestPrice = [[NSString stringWithFormat:@"%@" , stockDict[@"highestPrice"]] pz_stringNumberByMultiplyingBy:@"1"];
        highestPriceString = [NSString stringWithFormat:@"%@ %@" , @"最 高" , highestPrice];
        if (isUp) {
            ahighestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , highestPrice] changeColor:[UIColor colorWithHexString:stockRed] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:highestPriceString];
        }else{
            ahighestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , highestPrice] changeColor:[UIColor colorWithHexString:stockGreen] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:highestPriceString];
        }
    }else{
        highestPriceString = [NSString stringWithFormat:@"%@ %@" , @"最 高" , @"---"];
        if (isUp) {
            ahighestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:stockRed] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:highestPriceString];
        }else{
            ahighestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:stockGreen] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:highestPriceString];
        }
    }
    self.maxPrice.attributedText = ahighestPriceString;
    
    NSString *lowestPriceString;
    NSAttributedString *alowestPriceString;
    if (stockDict[@"lowestPrice"]) {
        NSString *lowestPrice = [[NSString stringWithFormat:@"%@" , stockDict[@"lowestPrice"]] pz_stringNumberByMultiplyingBy:@"1"];
        lowestPriceString = [NSString stringWithFormat:@"%@ %@" , @"最 低" , lowestPrice];
        if (isUp) {
            alowestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , lowestPrice] changeColor:[UIColor colorWithHexString:stockRed] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:lowestPriceString];
        }else{
            alowestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , lowestPrice] changeColor:[UIColor colorWithHexString:stockGreen] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:lowestPriceString];
        }
    }else{
        lowestPriceString = [NSString stringWithFormat:@"%@ %@" , @"最 低" , @"---"];
        if (isUp) {
            alowestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:stockRed] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:lowestPriceString];
        }else{
            alowestPriceString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:stockGreen] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:lowestPriceString];
        }
    }
    self.minPrice.attributedText =alowestPriceString;
    
    
    //成交额
    NSString *valueString;
    NSAttributedString *avalueString;
    if (stockDict[@"turnover"]) {
        NSString *value = [self formateValueWithString:[[NSString stringWithFormat:@"%@" , stockDict[@"turnover"]] pz_stringNumberByMultiplyingBy:@"1"]];
        valueString = [NSString stringWithFormat:@"%@ %@" , @"成交额" , value];
        
        avalueString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@"%@" , value] changeColor:[UIColor colorWithHexString:dark] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:valueString];
    }else{
        valueString = [NSString stringWithFormat:@"%@ %@" , @"成交额" , @"---"];
        avalueString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@"%@" , @"---"] changeColor:[UIColor colorWithHexString:dark] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:valueString];
    }
    self.value.attributedText =avalueString;
    
    
    

    //成交量
    NSString *volumeString;
    NSAttributedString *avolumeString;
    if (stockDict[@"volume"]) {
        NSString *volume = [self formateVolumeWithString:[[NSString stringWithFormat:@"%@" , stockDict[@"volume"]] pz_stringNumberByMultiplyingBy:@"1"]];
        volumeString = [NSString stringWithFormat:@"%@ %@" , @"成交量" , volume];
        
        avolumeString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , volume] changeColor:[UIColor colorWithHexString:dark] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:volumeString];
    }else{
        volumeString = [NSString stringWithFormat:@"%@ %@" , @"成交量" , @"---"];
        
        avolumeString = [NSString getAttriNeedChangeString:[NSString stringWithFormat:@" %@" , @"---"] changeColor:[UIColor colorWithHexString:dark] changeFont:[UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:volumeString];
    }
    self.volume.attributedText =avolumeString;
    
    
}

//成交手数转换
-(NSString *)formateVolumeWithString:(NSString *)volumeString
{
    CGFloat volume = [volumeString floatValue];
    CGFloat wVolume = volume/10000.f;
    NSString *text, *descText;
    if (wVolume > 1) {
        CGFloat yVolume = wVolume/10000.f;
        if (yVolume > 1) {
            text = [NSString stringWithFormat:@"%.2f",yVolume];
            descText = @"亿手";
        } else {
            text = [NSString stringWithFormat:@"%.2f",wVolume];
            descText = @"万手";
        }
    } else {
        text = [NSString stringWithFormat:@"%.0f",volume];
        descText = @"手";
    }
    
    return [NSString stringWithFormat:@"%@%@" , text , descText];
}
//成交金额转换
-(NSString *)formateValueWithString:(NSString *)valueString
{
    CGFloat value = [valueString floatValue];
    CGFloat wValue = value/10000.f;
    NSString *text, *descText;
    if (wValue > 1) {
        CGFloat yVolume = wValue/10000.f;
        if (yVolume > 1) {
            text = [NSString stringWithFormat:@"%.2f",yVolume];
            descText = @"亿元";
        } else {
            text = [NSString stringWithFormat:@"%.2f",wValue];
            descText = @"万元";
        }
    } else {
        text = [NSString stringWithFormat:@"%.0f",value];
        descText = @"元";
    }
    
    return [NSString stringWithFormat:@"%@%@" , text , descText];
}






-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initConfig];
        
        [self openPrice];
        [self yesClosePrice];
        [self maxPrice];
        [self minPrice];
        [self priceSpeed];
        [self value];
        [self volume];
        
    }
    return self;
}
-(void)initConfig
{
    self.marginLR = rate375(15);
    self.margin = rate375(30);
    self.labelWidth = (LKScreenW - self.marginLR * 2 - self.margin * 2) / 3;
    self.marginTB = isPhoneX ? rateX812(8) : rate750(8);
}

+(instancetype)stockMarketDiskDataViewWithStockDict:(NSDictionary *)stockDict
{
    YDStockMarketDiskDataView *detailView = [[YDStockMarketDiskDataView alloc] init];
    detailView.stockDict = stockDict;
    return detailView;
}
//blueyGrey
-(UILabel *)openPrice
{
    if (_openPrice == nil) {
        UILabel *openPrice = [[UILabel alloc] init];
        [self addSubview:openPrice];
        openPrice.textColor = [UIColor colorWithHexString:blueyGrey];
        openPrice.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        openPrice.textAlignment = NSTextAlignmentLeft;
        openPrice.text = @"今 开 ---";
        [openPrice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(self.marginLR);
            make.top.equalTo(self).offset(self.marginTB);
            make.width.equalTo(@(self.labelWidth));
        }];
        openPrice.adjustsFontSizeToFitWidth = YES;
        _openPrice = openPrice;
    }
    return _openPrice;
}

-(UILabel *)yesClosePrice
{
    if (_yesClosePrice == nil) {
        UILabel *yesClosePrice = [[UILabel alloc] init];
        [self addSubview:yesClosePrice];
        yesClosePrice.textColor = [UIColor colorWithHexString:blueyGrey];
        yesClosePrice.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        yesClosePrice.textAlignment = NSTextAlignmentLeft;
        yesClosePrice.text = @"昨 收 ---";
        [yesClosePrice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.openPrice);
            make.top.equalTo(self.openPrice.mas_bottom).offset(self.marginTB);
            make.width.equalTo(@(self.labelWidth));
        }];
        yesClosePrice.adjustsFontSizeToFitWidth = YES;
        _yesClosePrice = yesClosePrice;
    }
    return _yesClosePrice;
}

-(UILabel *)maxPrice
{
    if (_maxPrice == nil) {
        UILabel *maxPrice = [[UILabel alloc] init];
        [self addSubview:maxPrice];
        maxPrice.textColor = [UIColor colorWithHexString:blueyGrey];
        maxPrice.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        maxPrice.textAlignment = NSTextAlignmentLeft;
        maxPrice.text = @"最 高 ---";
        [maxPrice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.openPrice.mas_right).offset(self.margin);
            make.top.equalTo(self.openPrice.mas_top);
            make.width.equalTo(@(self.labelWidth));
        }];
        maxPrice.adjustsFontSizeToFitWidth = YES;
        _maxPrice = maxPrice;
    }
    return _maxPrice;
}
-(UILabel *)minPrice
{
    if (_minPrice == nil) {
        UILabel *minPrice = [[UILabel alloc] init];
        [self addSubview:minPrice];
        minPrice.textColor = [UIColor colorWithHexString:blueyGrey];
        minPrice.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        minPrice.textAlignment = NSTextAlignmentLeft;
        minPrice.text = @"最 低 ---";
        [minPrice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.maxPrice);
            make.top.equalTo(self.maxPrice.mas_bottom).offset(self.marginTB);
            make.width.equalTo(@(self.labelWidth));
        }];
        minPrice.adjustsFontSizeToFitWidth = YES;
        _minPrice = minPrice;
    }
    return _minPrice;
}
-(UILabel *)value
{
    if (_value == nil) {
        UILabel *value = [[UILabel alloc] init];
        [self addSubview:value];
        value.textColor = [UIColor colorWithHexString:blueyGrey];
        value.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        value.textAlignment = NSTextAlignmentLeft;
        value.text = @"成交额 ---";
        [value mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.maxPrice.mas_right).offset(self.margin);
            make.top.equalTo(self.openPrice.mas_top);
            make.width.equalTo(@(self.labelWidth));
        }];
        value.adjustsFontSizeToFitWidth = YES;
        _value = value;
    }
    return _value;
}

-(UILabel *)volume
{
    if (_volume == nil) {
        UILabel *volume = [[UILabel alloc] init];
        [self addSubview:volume];
        volume.textColor = [UIColor colorWithHexString:blueyGrey];
        volume.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        volume.textAlignment = NSTextAlignmentLeft;
        volume.text = @"成交量 ---";
        [volume mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.value.mas_left);
            make.top.equalTo(self.value.mas_bottom).offset(self.marginTB);
            make.width.equalTo(@(self.labelWidth));
        }];
        volume.adjustsFontSizeToFitWidth = YES;
        _volume = volume;
    }
    return _volume;
}

@end
